package cn.jtfadmin.core.modules.sys.service.impl;

import cn.jtfadmin.base.lang.common.security.UserDetails;
import cn.jtfadmin.base.lang.common.service.BaseService;
import cn.jtfadmin.base.lang.common.utils.RequestUtils;
import cn.jtfadmin.base.lang.config.user.UserHolder;
import cn.jtfadmin.core.config.shiro.UserDetailsPropertiesKey;
import cn.jtfadmin.core.enums.LogTypeEnum;
import cn.jtfadmin.core.enums.OperateTypeEnum;
import cn.jtfadmin.core.modules.sys.domain.dto.SysUserModelDTO;
import cn.jtfadmin.core.modules.sys.domain.entity.SysLogEntity;
import cn.jtfadmin.core.modules.sys.service.SysLogActionService;
import cn.jtfadmin.core.modules.sys.utils.SysUserDetailUtil;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.cache.CacheManager;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;


/**
 * 系统日志
 */
@Service
@Validated
public class SysLogActionServiceImpl implements SysLogActionService {

    @Autowired
    private BaseService<SysLogEntity> service;

    @Autowired
    private CacheManager cacheManager;


    private String cacheName = "sys_log_url_info";

    @Override
    public void logLoginSuccess(UserDetails userDetails) {
        HttpServletRequest request = RequestUtils.getRequest();
        SysLogEntity sysLogEntity = new SysLogEntity();
        sysLogEntity.setLogType(LogTypeEnum.LOGIN);
        sysLogEntity.setCostTime(0L);
        sysLogEntity.setIp(RequestUtils.getIpAddr(request));
        sysLogEntity.setRequestType(request.getContentType());
        sysLogEntity.setRequestUrl(request.getRequestURL().toString());
        sysLogEntity.setMethod(request.getMethod());
        sysLogEntity.setUsername(UserHolder.getUserDetails().getDisplayName());
        sysLogEntity.setUserId(UserHolder.getUserId());
        sysLogEntity.setLogContent("用户登录");
        SysUserModelDTO userModelDTO = SysUserDetailUtil.getUserModelDTO(UserHolder.getUserDetails());
        if(Objects.nonNull(userModelDTO)){
            sysLogEntity.setOrgName(userModelDTO.getSysDepartName());
            sysLogEntity.setOrgId(userModelDTO.getSysDepartId());
        }

        sysLogEntity.setOperateType(OperateTypeEnum.ADD);
        service.save(sysLogEntity);
    }

    @Override
    public void logLoginout(UserDetails userDetails) {
        HttpServletRequest request = RequestUtils.getRequest();
        SysLogEntity sysLogEntity = new SysLogEntity();
        sysLogEntity.setLogType(LogTypeEnum.LOGIN);
        sysLogEntity.setCostTime(0L);
        sysLogEntity.setIp(RequestUtils.getIpAddr(request));
        sysLogEntity.setRequestType(request.getContentType());
        sysLogEntity.setRequestUrl(request.getRequestURL().toString());
        sysLogEntity.setMethod(request.getMethod());
        sysLogEntity.setUsername(UserHolder.getUserDetails().getDisplayName());
        sysLogEntity.setUserId(UserHolder.getUserId());
        sysLogEntity.setLogContent("用户退出");
        SysUserModelDTO userModelDTO = SysUserDetailUtil.getUserModelDTO(UserHolder.getUserDetails());
        if(Objects.nonNull(userModelDTO)){
            sysLogEntity.setOrgName(userModelDTO.getSysDepartName());
            sysLogEntity.setOrgId(userModelDTO.getSysDepartId());
        }
        sysLogEntity.setOperateType(OperateTypeEnum.ADD);
        service.save(sysLogEntity);
    }


    @Override
    public void newOperateLog(String logContent, Long costTime, OperateTypeEnum logOperateTypeEnum, String param, Exception e) {
        HttpServletRequest request = RequestUtils.getRequest();
        SysLogEntity sysLogEntity = new SysLogEntity();
        sysLogEntity.setLogContent(logContent);
        sysLogEntity.setLogType(LogTypeEnum.OPERATE);
        sysLogEntity.setCostTime(costTime);
        sysLogEntity.setRequestParam(param);
        sysLogEntity.setIp(RequestUtils.getIpAddr(request));
        sysLogEntity.setRequestType(request.getContentType());
        sysLogEntity.setRequestUrl(request.getRequestURL().toString());
        sysLogEntity.setMethod(request.getMethod());
        sysLogEntity.setOperateType(logOperateTypeEnum);
        sysLogEntity.setUsername(UserHolder.getUserDetails().getDisplayName());
        sysLogEntity.setUserId(UserHolder.getUserId());
        UserDetails userDetails = UserHolder.getUserDetails();
        if (userDetails != null) {
            SysUserModelDTO userModelDTO = SysUserDetailUtil.getUserModelDTO(UserHolder.getUserDetails());
            if(Objects.nonNull(userModelDTO)){
                sysLogEntity.setOrgName(userModelDTO.getSysDepartName());
                sysLogEntity.setOrgId(userModelDTO.getSysDepartId());
            }
        }
        if (Objects.nonNull(e)) {
            sysLogEntity.setExContent(ExceptionUtils.getStackTrace(e));
        }
        service.save(sysLogEntity);
    }



}
